cmake_minimum_required(VERSION 2.8.3)

#set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)

project(Toucan)

#Find wxWidgets
find_package(wxWidgets COMPONENTS net adv aui stc scintilla core base REQUIRED)
include(${wxWidgets_USE_FILE})

#Find boost
#set(BOOST_ROOT "E:/dev/tools/boost_1_54_0")
set(BOOST_ROOT "" CACHE PATH "Path to directory with Boost library")
set(Boost_USE_STATIC_LIBS "ON")
#find_package(Boost REQUIRED)
find_package(Boost COMPONENTS thread system REQUIRED)
add_definitions(-DBOOST_DATE_TIME_NO_LIB)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

#Find Lua
find_package(Lua51 REQUIRED)
include_directories(${LUA_INCLUDE_DIR})

#Find SWIG
find_package(SWIG REQUIRED)

#Find Google Test
find_package(GTest)

if(MSVC)
	#Ask if we are using a static CRT
	set(Toucan_Static FALSE CACHE BOOL "Should we use the static CRT?")
    #Disable security warnings
    add_definitions(-D_CRT_SECURE_NO_WARNINGS)
    #Set the warning level to maximum
    if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
        string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    endif(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")

	if(Toucan_Static)
    #We statically link to reduce dependancies
		foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE)
			if(${flag_var} MATCHES "/MD")
				string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
			endif(${flag_var} MATCHES "/MD")
		endforeach(flag_var)
	endif(Toucan_Static)
endif(MSVC)

if(MINGW)
	# Under MinGW we want to add these flags
	# -std=gnu++11 enables C++11 support with GNU extensions (otherwise it doesn't compile)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
endif(MINGW)

#Traverse through the sub-directories
add_subdirectory(backup)
add_subdirectory(controls)
add_subdirectory(data)
add_subdirectory(forms)
add_subdirectory(secure)
add_subdirectory(sync)
add_subdirectory(test)

#Link against them
link_directories(backup)
link_directories(controls)
link_directories(data)
link_directories(forms)
link_directories(secure)
link_directories(sync)

#toucan_wrap.cpp is generated by SWIG
set_source_files_properties(toucan_wrap.cpp PROPERTIES GENERATED true)

#Add the source and header files
set(source ${source} basicfunctions.cpp dragndrop.cpp filecounter.cpp fileops.cpp)
set(source ${source} job.cpp log.cpp luamanager.cpp luathread.cpp path.cpp rules.cpp settings.cpp)
set(source ${source} signalprocess.cpp toucan.cpp toucan_wrap.cpp)

set(headers ${headers} basicfunctions.h dragndrop.h filecounter.h fileops.h)
set(headers ${headers} job.h log.h luamanager.h luathread.h path.h rules.h settings.h)
set(headers ${headers} signalprocess.h toucan.h)
set(headers ${headers} toucan.i typemaps.i)

if(WIN32)
    #Add the resource file for the icon
    set(source ${source} toucan.rc)
endif(WIN32)

#Generate toucan_wrap.cpp using swig
add_custom_command(
    OUTPUT  ${CMAKE_CURRENT_SOURCE_DIR}/toucan_wrap.cpp
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/toucan.i 
    COMMAND ${SWIG_EXECUTABLE} -o ${CMAKE_CURRENT_SOURCE_DIR}/toucan_wrap.cpp -lua -c++ ${CMAKE_CURRENT_SOURCE_DIR}/toucan.i
)

#Set up the exe
add_executable(toucan ${source} ${headers})
#target_link_libraries(toucan forms backup controls data secure sync ${wxWidgets_LIBRARIES} ${LUA_LIBRARIES})
target_link_libraries(toucan forms backup controls data secure sync ${wxWidgets_LIBRARIES} ${LUA_LIBRARIES} ${Boost_LIBRARIES})

#Once we have built Toucan move it somewhere suitable
set(Toucan_Output "" CACHE PATH "The path to an existing Toucan installation")
set(Toucan_Exepath ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/toucan${CMAKE_EXECUTABLE_SUFFIX})
add_custom_command(
    TARGET toucan
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy ${Toucan_Exepath} ${Toucan_Output}/App/Toucan/toucan${CMAKE_EXECUTABLE_SUFFIX}
)
